Dominos = LibStub("AceAddon-3.0"):NewAddon("Dominos", "AceConsole-3.0", "AceEvent-3.0", "AceComm-3.0")

-- ---------------------------------------------------------------------------------
-- DB SETTINGS
-- ---------------------------------------------------------------------------------
local defaults = {
	profile = {
		frameOptions = {
			targetFrame = {
				size = 25,
				bgOpacity = 50,
				fgOpacity = 100,
				borderOpacity = 100,
			},
			queFrame = {
				size = 20,
				bgOpacity = 50,
				fgOpacity = 100,
				borderOpacity = 100,
			},
			backgroundOptions = {
				showOnActive = true,
				showOnInactive = false,
				active = {
					color = {
						r = 1,
						g = 1,
						b = 1,
						a =0.8,
					},
				},
				inactive = {
					color = {
						r = 1,
						g = 1,
						b = 1,
						a =0.25,
					},
				},
			},
			frameSpacing = 2,
			anchorDirection = "Right",
			locked = false,
			position = {
				x = 0,
				y = 0,
			},
		},
		killOrder = {
			8,	-- Skull
			7,	-- X
			1,	-- Star
			2,	-- Circle
			3,	-- Diamond
			4,	-- Triangle
			5,	-- Moon
			6	-- Square
		},
	},
}


-- ---------------------------------------------------------------------------------
-- SCOPED VARIABLES
-- ---------------------------------------------------------------------------------
local Dominos = Dominos

Dominos.MouseDownFrame = nil
Dominos.IsDragging = false

Dominos.Frames = {}
Dominos.ActiveIconTable = {}
Dominos.ActiveKillOrderTable = {}
Dominos.CommPrefix = "Dominos"
Dominos.PlayerName = UnitName("player")

Dominos.RaidIconInfoTable = {
	{color = "yellow",	icon = "Star"},		-- 1
	{color = "orange",	icon = "Cricle"},	-- 2
	{color = "purple",	icon = "Diamond"},	-- 3
	{color = "green",	icon = "Triangle"},	-- 4
	{color = "silver",	icon = "Moon"},		-- 5
	{color = "blue",	icon = "Square"},	-- 6
	{color = "red",		icon = "X"},		-- 7
	{color = "white",	icon = "Skull"}		-- 8
}

Dominos.bit = {}
Dominos.bit.RAID_TARGET_FLAGS = _G.bit.bor(
	COMBATLOG_OBJECT_RAIDTARGET1,
	COMBATLOG_OBJECT_RAIDTARGET2,
	COMBATLOG_OBJECT_RAIDTARGET3,
	COMBATLOG_OBJECT_RAIDTARGET4,
	COMBATLOG_OBJECT_RAIDTARGET5,
	COMBATLOG_OBJECT_RAIDTARGET6,
	COMBATLOG_OBJECT_RAIDTARGET7,
	COMBATLOG_OBJECT_RAIDTARGET8
)

Dominos.IconCoords = {
	-- 1: yellow star (0, 0.25, 0, 0.25)
	[1] = {
		minX = 0,
		maxX = 0.25,
		minY = 0,
		maxY = 0.25
	},
	-- 2: orange circle (0.25, 0.5, 0, 0.25)
	[2] = {
		minX = 0.25,
		maxX = 0.5,
		minY = 0,
		maxY = 0.25
	},
	-- 3: purple diamond (0.5, 0.75, 0, 0.25)
	[3] = {
		minX = 0.5,
		maxX = 0.75,
		minY = 0,
		maxY = 0.25
	},
	-- 4: green triangle (0.75, 1, 0, 0.25)
	[4] = {
		minX = 0.75,
		maxX = 1,
		minY = 0,
		maxY = 0.25
	},
	-- 5: silver moon (0, 0.25, 0.25, 0.5)
	[5] = {
		minX = 0,
		maxX = 0.25,
		minY = 0.25,
		maxY = 0.5
	},
	-- 6: blue square (0.25, 0.5, 0.25, 0.5)
	[6] = {
		minX = 0.25,
		maxX = 0.5,
		minY = 0.25,
		maxY = 0.5
	},
	-- 7: red x (0.5, 0.75, 0.25, 0.5)
	[7] = {
		minX = 0.5,
		maxX = 0.75,
		minY = 0.25,
		maxY = 0.5
	},
	-- 8: wite skull (0.75, 1, 0.25, 0.5)
	[8] = {
		minX = 0.75,
		maxX = 1,
		minY = 0.25,
		maxY = 0.5
	}
}



-- ---------------------------------------------------------------------------------
-- EVENT HANDLERS
-- ---------------------------------------------------------------------------------
function Dominos:OnInitialize()
	self.db = LibStub("AceDB-3.0"):New("DominosDB", defaults, "Default")
	self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Dominos", "Dominos")
	self:RegisterChatCommand("dominos", "OnChatCommand")
	self:RegisterChatCommand("dom", "OnChatCommand")
	self:RegisterComm("Dominos", "RecievedGroupMessage")
	self:Print("player name: ", self.PlayerName)
end

function Dominos:OnEnable()
	self:Print("Dominos Enabled")
	self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
	self:RegisterEvent("RAID_TARGET_UPDATE")
	
	self:CreateFrames()
	self:RegisterEvent("UPDATE_MOUSEOVER_UNIT")
end

function Dominos:COMBAT_LOG_EVENT_UNFILTERED(event, timeStamp, eventType, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags)
	-- self:Print("COMBAT_LOG_EVENT")
	if eventType == "UNIT_DIED" then
		local hasFlags = self:HasFlags(dstFlags, self.bit.RAID_TARGET_FLAGS)
		if hasFlags then
			local idx = self:GetRaidFlagIndex(dstFlags)
			self:RemoveIcon(idx)
		end	
	end
end

function Dominos:RAID_TARGET_UPDATE()
	-- self:Print("RAID_TARGET_UPDATE")
	self:CheckLeaderTargets()
end

function Dominos:UPDATE_MOUSEOVER_UNIT()
	-- self:Print("UPDATE_MOUSEOVER_UNIT")
	
	local idx = GetRaidTargetIndex("mouseover")
	if idx and self:ShouldAddIconOnUnit("mouseover") then -- UnitCanAttack("player", "mouseover") and not UnitIsDead("mouseover") then
		local guid = UnitGUID("mouseover")
		self:AddIcon(idx, guid)
	end

	-- -------------------------------------
	-- debug: quickly add icons on mouseover
	if false then 
		if UnitCanAttack("player", "mouseover")
			and not UnitIsDead("mouseover")
			and not GetRaidTargetIndex("mouseover") then
		
			local idx = self:GetNextIcon()
			if idx then
				SetRaidTarget("mouseover", idx)
				self:AddIcon(idx, UnitGUID("mouseover"))
			end
		end
	end
end

function Dominos:OnChatCommand(input)
	if not input or input:trim() == "" then
		LibStub("AceConfigDialog-3.0"):Open("Dominos")
		-- InterfaceOptionsFrame_OpenToFrame(self.optionsFrame)
	else
		LibStub("AceConfigCmd-3.0").HandleCommand(Dominos, "dom", "dominos", input)
	end
end

function Dominos:RecievedGroupMessage(prefix, message, distribution, sender)	
	self:Print("[", message, "] recieved from [", sender, "]")
	-- if true then -- comment this out for final
	if sender ~= self.PlayerName then	
		local args = self:GetArgs(message)
		if args[1] == "KO" then
			if sender == self:GetLeaderName() then
				for x = 1, 8 do
					self.db.profile.killOrder[x] = tonumber(args[x+1])
				end
				self:UpdateActiveKillOrderTable()
			end
		end
	end
	
end

-- ---------------------------------------------------------------------------------
-- MAIN FUNCTIONS
-- ---------------------------------------------------------------------------------
function Dominos:AddIcon(idx, guid)
	local guidIdx = self:GetGUIDIndex(guid)
	
	if guidIdx and guidIdx ~= idx then
		self:RemoveIcon(idx)
	end
	
	self.ActiveIconTable[idx] = guid
	self:UpdateActiveKillOrderTable()
end

function Dominos:RemoveIcon(idx)
	-- self:Print("Removing Icon ", idx)
	self.ActiveIconTable[idx] = nil
	self:UpdateActiveKillOrderTable()
end

function Dominos:GetFrameIcon(killIdx)
	idx = self.ActiveKillOrderTable[killIdx]
	if idx then
		return idx
	else
		return 0
	end
end

function Dominos:ResetIconTable()
	self.ActiveIconTable = {}
end

function Dominos:CheckLeaderTargets()
	local numRaid = GetNumRaidMembers()
	local numParty = GetNumPartyMembers()

	-- if the player is in a party
	if numParty > 0 then
		if IsPartyLeader() then
			self:HandleUnitTargetAddRemove("target")
		else
			for x = 1, 4 do
				self:HandleUnitTargetAddRemove("party" .. x .. "target")
			end
		end
	end

	-- if the player is in a raid
	if numRaid > 0 then
		for x = 1, 40 do
			local _, rank = GetRaidRosterInfo(x)
			
			if rank > 0 then
				self:HandleUnitTargetAddRemove("raid" .. x .. "target")
			end
		end
	end
end


function Dominos:HandleUnitTargetAddRemove(unit)
	if self:ShouldAddIconOnUnit(unit) then
		local guid = UnitGUID(unit)
		local iconIdx = GetRaidTargetIndex(unit)
		
		if iconIdx then
			self:AddIcon(iconIdx, guid)
		else
			local guidIdx = self:GetGUIDIndex(guid)
			if guidIdx then
				self:RemoveIcon(guidIdx)
			end
		end
	end
end

function Dominos:GetGUIDIndex(guid)
	if guid then
		for x = 1, 8 do
			if self.ActiveIconTable[x] == guid then
				return x
			end
		end
	end
	return nil
end


function Dominos:UpdateActiveKillOrderTable()
	-- self:Print("updating active kill order table")

	self.ActiveKillOrderTable = {}
	for k, v in ipairs(self.db.profile.killOrder) do
		if self.ActiveIconTable[v] then
			table.insert(self.ActiveKillOrderTable, v)
		end
	end

	self:UpdateFrames()
end

function Dominos:ShouldAddIconOnUnit(targetUnit)
	if UnitCanAttack("player", targetUnit) and not UnitIsDead(targetUnit) then
		return true
	else
		return false
	end
end

-- ***********************************
-- All Manual Reorders go through here
-- ***********************************
function Dominos:ReorderKillOrderByIndex(moveFromIndex, moveToIndex)

	local moveFromValue = self.db.profile.killOrder[moveFromIndex]
	table.remove(self.db.profile.killOrder, moveFromIndex)
	table.insert(self.db.profile.killOrder, moveToIndex, moveFromValue)
	
	self:KillOrderChanged()
end

function Dominos:ReorderKillOrderByIcon(moveFromIcon, moveToIcon)
	local moveFromIndex = nil
	local moveToIndex = nil
	
	for k, v in ipairs(self.db.profile.killOrder) do
		if v == moveFromIcon then
			moveFromIndex = k
		elseif v == moveToIcon then
			moveToIndex = k
		end
	end
	
	if moveFromIndex and moveToIndex then
		self:ReorderKillOrderByIndex(moveFromIndex, moveToIndex)
	else
		self:Print("Error: could not find moveFrom or moveTo indices")
	end
end

function Dominos:KillOrderChanged()
	if IsRaidLeader() or IsPartyLeader() then
		self:SendCommMessage("Dominos", "KO "..self:GetKillOrderString(), self:GetGroupType(), nil, "ALERT")
	end
	
	self:UpdateActiveKillOrderTable()
end

function Dominos:GetKillOrderString()
	local str = ""
	for x = 1, 8 do
		str = str .. self.db.profile.killOrder[x] .. " "
	end
	return str
end

function Dominos:GetGroupType()
	local numRaid = GetNumRaidMembers()
	local numParty = GetNumPartyMembers()
	
	-- self:Print("numParty: ", numParty, " numRaid: ", numRaid)
	
	if numRaid > 0 then
		return "RAID"
	elseif numParty > 0 then
		return "PARTY"
	else
		return "SOLO"
	end
end

-- ---------------------------------------------------------------------------------
-- GET/SET FUNCTIONS
-- ---------------------------------------------------------------------------------

-- TargetFrameSize------------------------------------------------------------------
function Dominos:GetTargetFrameSize()
	return self.db.profile.frameOptions.targetFrame.size
end

function Dominos:SetTargetFrameSize(info, size)
	self.db.profile.frameOptions.targetFrame.size = size
	self:UpdateTargetFrameSize(size)
end
-- QueFrameSize---------------------------------------------------------------------
function Dominos:GetQueFrameSize()
	return self.db.profile.frameOptions.queFrame.size
end

function Dominos:SetQueFrameSize(info, size)
	self.db.profile.frameOptions.queFrame.size = size
	self:UpdateQueFrameSize()
end
-- AnchorDirection------------------------------------------------------------------
function Dominos:GetAnchorDirection()
	return self.db.profile.frameOptions.anchorDirection
end

function Dominos:SetAnchorDirection(info, direction)
	self.db.profile.frameOptions.anchorDirection = direction
	self:UpdateAnchors(direction)
end
-- FrameSpacing---------------------------------------------------------------------
function Dominos:GetFrameSpacing()
	return self.db.profile.frameOptions.frameSpacing
end

function Dominos:SetFrameSpacing(info, spacing)
	self.db.profile.frameOptions.frameSpacing = spacing
	self:UpdateAnchors()
end
-- ShowBackgroundOnActive-----------------------------------------------------------
function Dominos:GetShowBackgroundOnActive()
	return self.db.profile.frameOptions.backgroundOptions.showOnActive
end

function Dominos:SetShowBackgroundOnActive(info, v)
	self.db.profile.frameOptions.backgroundOptions.showOnActive = v
	self:UpdateBackgrounds()
end
-- ShowBackgroundOnInactive---------------------------------------------------------
function Dominos:GetShowBackgroundOnInactive()
	return self.db.profile.frameOptions.backgroundOptions.showOnInactive
end

function Dominos:SetShowBackgroundOnInactive(info, v)
	self.db.profile.frameOptions.backgroundOptions.showOnInactive = v
	self:UpdateBackgrounds()
end
-- Locked---------------------------------------------------------------------------
function Dominos:GetLocked()
	return self.db.profile.frameOptions.locked
end

function Dominos:SetLocked(info, locked)
	self.db.profile.frameOptions.locked = locked
	self:UpdateMovable()
end
-- Active Color----------------------------------------------------------
function Dominos:GetActiveColor()
	local color = self.db.profile.frameOptions.backgroundOptions.active.color
	return color.r, color.g, color.b, color.a
end

function Dominos:SetActiveColor(info, r, g, b, a)
	local color = self.db.profile.frameOptions.backgroundOptions.active.color
	color.r = r
	color.g = g
	color.b = b
	color.a = a
	self:UpdateBackgrounds()
end
-- Inactive Color----------------------------------------------------------
function Dominos:GetInactiveColor()
	local color = self.db.profile.frameOptions.backgroundOptions.inactive.color
	return color.r, color.g, color.b, color.a
end

function Dominos:SetInactiveColor(info, r, g, b, a)
	local color = self.db.profile.frameOptions.backgroundOptions.inactive.color
	color.r = r
	color.g = g
	color.b = b
	color.a = a
	self:UpdateBackgrounds()
end
-- ---------------------------------------------------------------------------------
-- ---------------------------------------------------------------------------------
-- ---------------------------------------------------------------------------------


-- ---------------------------------------------------------------------------------
-- FRAME EVENTS
-- ---------------------------------------------------------------------------------
function Dominos:Frame_OnDoubleClick(button, id)
	local icon = self:GetFrameIcon(id)
	if icon ~= 0 then
		self:ReorderKillOrderByIcon(icon, self:GetFrameIcon(1))
	end
end

function Dominos:Frame_OnMouseDown(button, id)
	if button == "RightButton" then
		self.MouseDownFrame = id
	end
end

function Dominos:Frame_OnMouseUp(button, id)
	if button == "RightButton" and not self.IsDragging then
		self:RemoveIcon(self:GetFrameIcon(id))
		self.MouseDownFrame = nil
	end
	
	self.IsDragging = false
	self.MouseDownFrame = nil

end

function Dominos:Frame_OnEnter(id)
	if self.MouseDownFrame and id ~= self.MouseDownFrame then
		self.IsDragging = true
		local ko = self.db.profile.killOrder		
		local dragedIcon = self:GetFrameIcon(self.MouseDownFrame)
		local destIcon = self:GetFrameIcon(id)
		
		if destIcon ~= 0 then
			self:ReorderKillOrderByIcon(dragedIcon, destIcon)
			self.MouseDownFrame = id
		end
	end
	
end

-- ---------------------------------------------------------------------------------
-- FRAME FUNCTIONS
-- ---------------------------------------------------------------------------------


-- --------------
-- Update
function Dominos:UpdateFrames()
	self:UpdateBackgrounds()
	self:UpdateBorders()
	self:UpdateIcons()
end

-- --------------
-- Create
function Dominos:CreateFrames()
	-- self:Print("creating frames")
	
	-- main frame
	self.Frames[1] = self:CreateFrame(UIParent)
	self.Frames[1].frame:SetScript("OnDoubleClick", function(_, button) self:Frame_OnDoubleClick(button, "target") end)
	self.Frames[1].frame:SetScript("OnMouseDown", function(_, button) self:Frame_OnMouseDown(button, 1) end)
	self.Frames[1].frame:SetScript("OnMouseUp", function(_, button) self:Frame_OnMouseUp(button, 1) end)
	self.Frames[1].frame:SetScript("OnEnter", function() self:Frame_OnEnter(1) end)
	
	-- que frames
	for x = 2, 8 do
		ft = self:CreateFrame(self.Frames[x-1].frame)
		-- ft.frame:RegisterForDrag("LeftButton")
		-- click
		ft.frame:SetScript("OnDoubleClick", function(_, button) self:Frame_OnDoubleClick(button, x) end)
		-- mouse
		ft.frame:SetScript("OnMouseDown", function(_, button) self:Frame_OnMouseDown(button, x) end)
		ft.frame:SetScript("OnMouseUp", function(_, button) self:Frame_OnMouseUp(button, x) end)
		-- enter
		ft.frame:SetScript("OnEnter", function() self:Frame_OnEnter(x) end)
		self.Frames[x] = ft
	end
	
	self:InitializeFrameSettings()
	self:UpdateFrames()
end

function Dominos:CreateFrame(parent)
	local f = CreateFrame("Button", nil, parent)
	f:RegisterForClicks("LeftButtonUp", "RightButtonUp")
	-- f:EnableMouse(true)
	
	-- this can probably get removed since its in the anchors section
	-- f:SetPoint("TOPLEFT", "UIParent", "CENTER", 50, 50)

	-- this needs to be pulled out and put into the UpdateBackgrounds() method
	if true then
		f:SetBackdrop({bgFile = "Interface\\TutorialFrame\\TutorialFrameBackground"})
		f:SetBackdropColor(1,1,1,0.8)
	end

	
	local t = f:CreateTexture(nil, "ARTWORK")
	t:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons")
	t:SetPoint("CENTER", f)
	
	-- this can probably get removed as its part of the update size method
	t:SetWidth(50)
	t:SetHeight(50)
	
	return {frame = f, texture = t}
end

-- --------------
-- Movable
function Dominos:UpdateMovable()
	if not self.db.profile.frameOptions.locked then
		local f = self.Frames[1].frame
		f:RegisterForDrag("LeftButton")
		f:SetMovable(true)
		f:SetScript("OnDragStart", function()
			this:StartMoving()
			this.isMoving = true
			end)
		f:SetScript("OnDragStop", function()
			local _, _, _, x, y = this:GetPoint() -- self.Frames[1].frame:GetPoint(1)
			self.db.profile.frameOptions.position.x = x
			self.db.profile.frameOptions.position.y = y
			this:StopMovingOrSizing()
			this.isMoving = false
			end)
	else
		local f = self.Frames[1].frame
		f:RegisterForDrag()
		f:SetMovable(false)
		f:SetScript("OnDragStart", nil)
		f:SetScript("OnDragStop", nil)
	end
end

-- --------------
-- Frame Size
function Dominos:UpdateTargetFrameSize()
	local size = self.db.profile.frameOptions.targetFrame.size
	
	self.Frames[1].frame:SetWidth(size)
	self.Frames[1].frame:SetHeight(size)
	self.Frames[1].texture:SetWidth(size)
	self.Frames[1].texture:SetHeight(size)
end

function Dominos:UpdateQueFrameSize()
	local size = self.db.profile.frameOptions.queFrame.size
	for x = 2, 8 do
		self.Frames[x].frame:SetWidth(size)
		self.Frames[x].frame:SetHeight(size)
		self.Frames[x].texture:SetWidth(size)
		self.Frames[x].texture:SetHeight(size)
	end
end

-- --------------
-- Anchors
function Dominos:UpdateAnchors()
	local point = self:GetPointType()
	local relPoint = self:GetRelPointType()
	local offsetX, offsetY = self:GetAnchorOffsets()

	for x = 2, 8 do
		self.Frames[x].frame:ClearAllPoints()
		self.Frames[x].frame:SetPoint(point, self.Frames[x-1].frame, relPoint, offsetX, offsetY)
	end

end

function Dominos:GetPointType()
	local anchorDirection = self.db.profile.frameOptions.anchorDirection
	local directionTable = {
		Up = "BOTTOM",
		Down = "TOP",
		Left = "RIGHT",
		Right = "LEFT",
	}
	return directionTable[anchorDirection]
	
end

function Dominos:GetRelPointType()
	local anchorDirection = self.db.profile.frameOptions.anchorDirection
	local directionTable = {
		Up = "TOP",
		Down = "BOTTOM",
		Left = "LEFT",
		Right = "RIGHT",
	}
	return directionTable[anchorDirection]
end

function Dominos:GetAnchorOffsets()
	local anchorDirection = self.db.profile.frameOptions.anchorDirection
	local offsetAmount = self.db.profile.frameOptions.frameSpacing
	local directionTable = {
		Up =	{x = 0,		y = 1},
		Down =	{x = 0,		y = -1},
		Left =	{x = -1,	y = 0},
		Right =	{x = 1,		y = 0},
	}
	
	return (offsetAmount * directionTable[anchorDirection].x), (offsetAmount * directionTable[anchorDirection].y)
end

-- --------------
-- Background
function Dominos:UpdateBackgrounds()
	for x = 1, 8 do
		-- local showActive = self.db.profile.frameOptions.backgroundOptions.showOnActive
		-- local showInactive = self.db.profile.frameOptions.backgroundOptions.showOnInactive
		
		local showActive = self.db.profile.frameOptions.backgroundOptions.showOnActive
		local showInactive = self.db.profile.frameOptions.backgroundOptions.showOnInactive
		
		local f = self.Frames[x].frame
		
		
		if self.ActiveKillOrderTable[x] then  -- if active
			if showActive then
				self:ShowBackground(f, true, true)
			else
				self:ShowBackground(f, false, true)
			end
		else -- if Inactive
			if showInactive then
				self:ShowBackground(f, true, false)
			else
				self:ShowBackground(f, false, false)
			end
		end
	end
end

function Dominos:ShowBackground(frame, show, isActive)
	if show then
		local color
		if isActive then
			color = self.db.profile.frameOptions.backgroundOptions.active.color
		else
			color = self.db.profile.frameOptions.backgroundOptions.inactive.color		
		end

		-- frame:SetBackdrop({bgFile = "Interface\\TutorialFrame\\TutorialFrameBackground"})
		frame:SetBackdrop({bgFile = "Interface\\Tooltips\\UI-Tooltip-Background"})
		frame:SetBackdropColor(color.r, color.g, color.b, color.a)
	else
		frame:SetBackdrop(nil)
	end
end

-- --------------
-- Borders
function Dominos:UpdateBorders()

end

-- --------------
-- Icon
function Dominos:UpdateIcons()
	for x = 1, 8 do
		if self.ActiveKillOrderTable[x] then
			self:SetIcon(x, self.ActiveKillOrderTable[x])
		else
			self:SetIcon(x, 0)
		end
	end
end

function Dominos:SetIcon(frameIdx, iconIdx)
	if iconIdx > 0 then
		local coords = self.IconCoords[iconIdx]
		self.Frames[frameIdx].texture:Show()
		self.Frames[frameIdx].texture:SetTexCoord(coords.minX, coords.maxX, coords.minY, coords.maxY)
	else
		self.Frames[frameIdx].texture:Hide()
	end
end

-- --------------
-- Initalize
function Dominos:InitializeFrameSettings()
	local x = self.db.profile.frameOptions.position.x
	local y = self.db.profile.frameOptions.position.y
	self.Frames[1].frame:ClearAllPoints()
	self.Frames[1].frame:SetPoint("TOPLEFT", "UIParent", "TOPLEFT", x, y)

	self:UpdateTargetFrameSize()
	self:UpdateQueFrameSize()
	self:UpdateAnchors()
	self:UpdateMovable()
end

-- ---------------------------------------------------------------------------------
-- BIT FUNCTIONS
-- ---------------------------------------------------------------------------------
function Dominos:HasFlags(source, test)
	if _G.bit.band(source, test) > 0 then
		return true
	else
		return false
	end
end

function Dominos:HasFlag(source, test)
	if _G.bit.band(source, test) == test then
		return true
	else
		return false
	end
end

function Dominos:GetRaidFlagIndex(source)
	local idx = 0
	
	if self:HasFlag(source, COMBATLOG_OBJECT_RAIDTARGET1) then
		idx = 1
	elseif self:HasFlag(source, COMBATLOG_OBJECT_RAIDTARGET2) then
		idx = 2
	elseif self:HasFlag(source, COMBATLOG_OBJECT_RAIDTARGET3) then
		idx = 3
	elseif self:HasFlag(source, COMBATLOG_OBJECT_RAIDTARGET4) then
		idx = 4
	elseif self:HasFlag(source, COMBATLOG_OBJECT_RAIDTARGET5) then
		idx = 5
	elseif self:HasFlag(source, COMBATLOG_OBJECT_RAIDTARGET6) then
		idx = 6
	elseif self:HasFlag(source, COMBATLOG_OBJECT_RAIDTARGET7) then
		idx = 7
	elseif self:HasFlag(source, COMBATLOG_OBJECT_RAIDTARGET8) then
		idx = 8
	end
	
	return idx
end

-- ---------------------------------------------------------------------------------
-- UTILITY FUNCTIONS
-- ---------------------------------------------------------------------------------
function Dominos:GetArgs(string)
	local args = {}
	for n in string.gmatch(string, "%S+") do
		-- self:Print(n)
		table.insert(args, n)
	end
	return args
end

function Dominos:GetLeaderName()	
	local group = self:GetGroupType()
	
	if group == "PARTY" then
		if IsPartyLeader() then
			return self.PlayerName
		else
			return UnitName("party"..GetPartyLeaderIndex())
		end

	elseif group == "RAID" then
		if IsRaidLeader() then
			return self.PlayerName
		else
			for x = 1, 40 do
				name, rank = GetRaidRosterInfo(x)
				if rank == 2 then
					return name
				end
			end
		end

	elseif group == "SELF" then
		return nil
	end
end

function Dominos:PrintIconTable()
	local str = ""
	
	for x = 1, 8 do
		if self.ActiveIconTable[x] then
			str = str .. self.ActiveIconTable[x] .. " "
		else
			str = str .. "- "
		end
	end
	
	self:Print("Active Icons: " .. str)
	-- SendChatMessage(str)
end

function Dominos:PrintKillTable()
	local str = ""
	
	for x = 1, #self.db.profile.killOrder do
		str = str .. self.db.profile.killOrder[x]
	end
	
	self:Print("Kill Order: ", str)
end

function Dominos:PrintActiveKillTable()
	local str = ""
	
	for x = 1, #self.ActiveKillOrderTable do
		str = str .. self.ActiveKillOrderTable[x]
	end
	
	self:Print("Active Kill Order: ", str)
end

function Dominos:Test()
	self:Print("test")
end

